package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.entity.WmNews;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import com.heima.media.service.IWmNewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author WSJ
 * @Date 2022-03-09-14:50
 * @Version 1.0 （版本号）
 */

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    @Override
    public ResponseResult listByName(ChannelDto dto) {
        //构建分页条件
        IPage<WmChannel> iPage=new Page<>(dto.getPage(),dto.getSize());
        //构建查询条件
        LambdaQueryWrapper<WmChannel> wrapper=new LambdaQueryWrapper<>();
        if (dto.getName()!=null) {
            wrapper.like(WmChannel::getName,dto.getName());
        }
        wrapper.orderByAsc(WmChannel::getOrd);
        iPage = page(iPage, wrapper);
        //获取total
        long total = iPage.getTotal();
        //获取record
        List<WmChannel> channels = iPage.getRecords();
        //返回
        return new PageResponseResult(dto.getPage(), dto.getSize(), total, channels);
    }

    @Override
    public ResponseResult saveChannel(WmChannel channel) {
        //判断channel是否为空或者name字段为空
        if (channel==null|| StringUtils.isEmpty(channel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断name字段是否存在
        LambdaUpdateWrapper<WmChannel> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmChannel::getName,channel.getName());
        int count = count(wrapper);
        if (count>0) {
            //存在，则提示已存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //不存在，则保存
        //设置创建时间
        channel.setCreatedTime(new Date());
        //保存
        save(channel);
        //返回成功
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listByStatus() {
        //构建条件
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getStatus,1);
        wrapper.orderByAsc(WmChannel::getOrd);
        //查询
        List<WmChannel> wmChannels = list(wrapper);
        //返回
        return ResponseResult.okResult(wmChannels);

    }
    @Autowired
    private IWmNewsService newsService;
    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        //查询其他频道的名称
        LambdaQueryWrapper<WmChannel> query=new LambdaQueryWrapper<>();
        query.select(WmChannel::getName);
        query.ne(WmChannel::getId,wmChannel.getId());
        List<String> names = listObjs(query, o -> o.toString());
        //如果频道名称和其他频道冲突，返回提示
//        if (names.contains(wmChannel.getName())) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NAME_EXIST);
//        }
        if (!CollectionUtils.isEmpty(names)) {
            for (String name : names) {
                if (name.equalsIgnoreCase(wmChannel.getName())) {
                    return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_NAME_EXIST);
                }
            }
        }
        //如果频道修改为禁用，先判断频道是否被引用
        if (!(wmChannel.getStatus())) {
            LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(WmNews::getChannelId,wmChannel.getId());
            int count = newsService.count(wrapper);
            if (count>0) {
                //如果存在，则返回提示
                return ResponseResult.errorResult(AppHttpCodeEnum.NEWS_EXIST_IN_CHANNEL);
            }
        }
        WmChannel channel = new WmChannel();
        BeanUtils.copyProperties(wmChannel,channel);
        //修改
        updateById(channel);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //判断当前频道是否被启用，启用则无法删除
        WmChannel wmChannel = getById(id);
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_IS_ENABLE);
        }
        //禁用状态，删除
        removeById(id);
        return ResponseResult.okResult();
    }
}
